<!--
 * @Author: zhw 443844039@qq.com
 * @Date: 2025-07-31 23:49:28
 * @LastEditors: zhw 443844039@qq.com
 * @LastEditTime: 2025-08-24 22:50:34
 * @Description: 文档管理详情页面
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="60%"
    :before-close="handleClose"
    :destroy-on-close="true"
    :modal="true"
  >
    <el-form
      ref="formRef"
      class="form_item_w2"
      :model="pageForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="标题" prop="wordTitle">
        <el-input v-model="pageForm.wordTitle" placeholder="请输入标题" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="所属类别">
        <el-cascader v-model="pageForm.categoryId" :options="documentTree" :props="{emitPath:false}" :disabled="disabled"></el-cascader>
      </el-form-item>
      <el-form-item label="所属学院">
        <TheDeptSelect v-model="pageForm.collageId" :modelValue="pageForm.collageId" :deptType="39" placeholder="请选择所属学院"  :disabled="disabled" @update:modelValue="pageForm.collageId = $event"/>
      </el-form-item>
      <el-form-item label="所属专业">
        <TheDeptSelect v-model="pageForm.majorId" :modelValue="pageForm.majorId" :deptType="40" placeholder="请选择所属学院"  :disabled="disabled" @update:modelValue="pageForm.majorId = $event"/>
      </el-form-item>
      <el-form-item label="描述" class="item_w1">
        <el-input type="textarea" v-model="pageForm.description" placeholder="请输入描述" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="附件上传" class="item_w1">
        <FileUpload v-model="pageForm.fileId" :disabled="disabled" :fileInfo="pageForm.fileInfo"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading" v-if="action!='look'">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { systemDocumentAddApi,systemDocumentByIdApi, systemDocumentEditApi } from '@/api/system/document'
import FileUpload from "@/components/FileUpload";
import TheDeptSelect from '@/componentsView/TheDeptSelect.vue';

export default {
  name: 'DocumentDetail',
  components: { TheDeptSelect,FileUpload },
  data() {
    return {
      title:'详情',
      pageForm: {
        wordTitle:'',
        collageId: null
      },
      rules:{
        wordTitle: [
          { required: true, message: '请输入标题', trigger: 'change' },
        ]
      },
      visible:false,
      disabled:false,
      loading:false,
      action:'',
      documentTree:[],
    }
  },
  watch: {
    // 当弹窗关闭时重置表单
    visible(val) {
      if (!val) {
        this.$nextTick(() => {
          this.$refs.formRef?.resetFields()
        })
      }
    }
  },
  methods: {
    // 打开弹窗
    handleOpen(action, params) {
      this.action = action;
      this.disabled = action === 'look';
      this.visible = true;
      this.documentTree = params.treeData?params.treeData.map(node => this.transformNode(node)):[];
      if (action === 'edit' || action === 'look') {
        this.pageForm.wordId = params.wordId
        this.getSystemDocumentById();
      }
      if (action === 'add') {
        this.pageForm = {}
      }
    },

    transformNode(node) {
      // 创建新对象，只包含需要的属性
      const newNode = {
        label: node.label,
        value: node.id
      };
      // 如果有子节点，递归处理子节点
      if (node.children && node.children.length) {
        newNode.children = node.children.map(child => this.transformNode(child));
      }
      return newNode;
    },

    handleClose(){
      this.visible = false;
    },
    
    async getSystemDocumentById() {
      try {
        const res = await systemDocumentByIdApi(this.pageForm.wordId)
        const { wordId, wordTitle, deptId, description, categoryId, fileId, collageId, majorId, categoryName,fileInfo } = res.data
        this.pageForm = Object.assign({}, this.pageForm, {
          wordId,
          wordTitle,
          deptId,
          description,
          categoryId,
          categoryName,
          fileId,
          collageId,
          majorId,
          fileInfo
        })
      } catch (error) {
        this.$message.error(error.message || '获取文档详情失败')
      }
    },

    async handleSubmit() {
      const pureNumberRegex = /^[+-]?(\d+(\.\d*)?|\.\d+)$/;
      if (pureNumberRegex.test(this.pageForm.wordTitle)) {
        this.$message.error('标题不能为纯数字')
        return
      }
      try {
        await this.$refs.formRef.validate()
        this.loading = true
        const api = this.action === 'add' ? systemDocumentAddApi : systemDocumentEditApi
        const res = await api(this.pageForm)
        if (res.code === 200) {
          this.$emit('refresh')
          this.$message.success(res.msg)
          this.visible = false
        } else {
          this.$message.error(res.msg)
        }
      } catch (error) {
        this.$message.error(error.message || '操作失败')
      } finally {
        this.loading = false
      }
    }
  }
}
  
</script>
<style scoped lang="scss">
::v-deep .el-cascader{
  width: 100%;
}
</style>